package com.pactera.madp.cp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pactera.madp.cp.api.dto.emp.EmployeeFollowCreateDTO;
import com.pactera.madp.cp.api.dto.emp.EmployeeFollowUpdateDTO;
import com.pactera.madp.cp.api.entity.WorkEmployeeEntity;
import com.pactera.madp.cp.api.entity.employeeWechat.EmployeeFollowEntity;
import com.pactera.madp.cp.api.vo.employee.EmployeeFollowVO;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.mapper.ContactEmployeeTrackMapper;
import com.pactera.madp.cp.mapper.EmployeeFollowMapper;
import com.pactera.madp.cp.service.EmployeeFollowService;
import com.pactera.madp.cp.service.IContactService;
import com.pactera.madp.cp.service.IWorkContactService;
import com.pactera.madp.cp.service.IWorkEmployeeService;
import lombok.Data;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (CpEmployeeFollow)表服务实现类
 *
 * @author yushen.qi
 * @since 2021-06-29 15:06:39
 */
@Service
@Data
public class EmployeeFollowServiceImpl extends ServiceImpl<EmployeeFollowMapper, EmployeeFollowEntity> implements EmployeeFollowService {

    @Resource
    private IWorkEmployeeService iWorkEmployeeService;
    @Resource
    private ContactEmployeeTrackMapper contactEmployeeTrackMapper;
    @Resource
    private IWorkContactService iWorkContactService;
    @Resource
    private IContactService contactService;

    @Override
    public boolean insert(EmployeeFollowCreateDTO dto) {
        Integer corpId = CorpContextHolder.getCorpId();
        WorkEmployeeEntity one = iWorkEmployeeService.getOne(Wrappers.<WorkEmployeeEntity>lambdaQuery()
                .eq(WorkEmployeeEntity::getWxUserId, dto.getWxUserId())
                .eq(WorkEmployeeEntity::getCorpId, corpId));
        EmployeeFollowEntity employeeFollowEntity = new EmployeeFollowEntity();
        BeanUtil.copyProperties(dto, employeeFollowEntity);
        employeeFollowEntity.setCreateTime(LocalDateTime.now());
        employeeFollowEntity.setUpdateTime(LocalDateTime.now());
        employeeFollowEntity.setCorpId(corpId);
        employeeFollowEntity.setName(one.getName());
        return save(employeeFollowEntity);
    }

    @Override
    public boolean update(EmployeeFollowUpdateDTO dto) {
        EmployeeFollowEntity employeeFollowEntity = new EmployeeFollowEntity();
        employeeFollowEntity.setId(dto.getId());
        employeeFollowEntity.setFollowBody(dto.getFollowBody());
        return update(employeeFollowEntity,Wrappers.<EmployeeFollowEntity>lambdaQuery().eq(EmployeeFollowEntity::getId,dto.getId()));
    }

    @Override
    public void state(Integer id) {
        EmployeeFollowEntity employeeFollowEntity = new EmployeeFollowEntity();
        employeeFollowEntity.setId(id);
        employeeFollowEntity.setFollowState(1);
        update(employeeFollowEntity, Wrappers.<EmployeeFollowEntity>lambdaQuery().eq(EmployeeFollowEntity::getId,id));
    }

    @Override
    public IPage listByPage(Page page,String wxExternalUserid, String wxUserId) {
        Integer corpId = CorpContextHolder.getCorpId();
        IPage page1 = lambdaQuery().eq(EmployeeFollowEntity::getWxExternalUserid,wxExternalUserid).eq(EmployeeFollowEntity::getWxUserId,wxUserId).eq(EmployeeFollowEntity::getCorpId, corpId).page(page);
        Object collect = page1.getRecords().stream().map(entity -> {
            EmployeeFollowVO employeeFollowVO = new EmployeeFollowVO();
            BeanUtil.copyProperties(entity, employeeFollowVO);
            return employeeFollowVO;
        }).sorted(Comparator.comparing(EmployeeFollowVO::getCreateTime).reversed()).collect(Collectors.toList());
        page1.setRecords((List) collect);
        return page1;
    }

    @Override
    public boolean remove(Integer id) {
        return remove(Wrappers.<EmployeeFollowEntity>lambdaQuery().eq(EmployeeFollowEntity::getId,id));
    }
}
